<?php
namespace hotel\modules\apiadmin\controllers;

use hotel\common\models\OrdersModel;
use hotel\common\utils\OutputExecl;
use hotel\common\utils\Tool;
use \Yii;

/**
* 订单相关控制器
*/

class OrderController extends CoreController
{
	//订单列表
	public function actionOrderList()
	{
		$where = $this->formartWhere();
		$page  = $this->request('page','1');
		$params = array(
			'field'	=> ['id','order_sn','name','price','pay_price','phone','carno','order_state','des','rate','rmnum','arr','dep','create_time','pay_time','discount','coupons_type','couponid','couponname','coupon_from','couponname','rsvno','accnt','rmtype','ratecode'],
			'order' => 'id desc',
			'page'	=> $page,
			'limit' => $this->request('page_size',10),
		);
		$list = OrdersModel::getOrderList($where,$params);
		$pages = OrdersModel::$pages;
		$data['list'] = $list;
		foreach($list as $k=>$v){
			if($v['coupon_from'] == 1){
				if($v['coupons_type'] == 1){
					$list[$k]['coupons_type'] = '折扣券';
				}elseif($v['coupons_type'] == 2){
					$list[$k]['coupons_type'] = '现金券';
				}elseif($v['coupons_type'] == 3){
					$list[$k]['coupons_type'] = '满减券';
				}
			}elseif($v['coupon_from'] == 2){
				if($v['coupons_type'] == 1){
					$list[$k]['coupons_type'] = '满减券';
				}elseif($v['coupons_type'] == 2){
					$list[$k]['coupons_type'] = '折扣券';
				}elseif($v['coupons_type'] == 3){
					$list[$k]['coupons_type'] = '现金券';
				}
			}
		}
		$data['pageInfo'] = [
			'count' => $pages,
			'pageSize' => $params['limit'],
			'currPage' => $params['page'],
			'tatalPage' => ceil($pages/$params['limit']),
		];
		$orderState = Yii::$app->params['orderState'];
		return Tool::showRes([0, '订单列表'],$data,'',['orderState'=>$orderState]);
	}

	/*
		订单列表 条件组装
	*/
	public function formartWhere()
	{
		$where = [];
		$whereAnd = [];
		$where['appid'] = $this->appid;
		$searchKeys = json_decode($this->request('search'),1);
		foreach($searchKeys as $k=>$val)
		{
			if($k != 'order_state'){
				if(!$val) continue;
			}
			if($k=='date')
			{
				if(!$val['0'] || !$val['1']) continue;
				$whereAnd[] = ['between', 'create_time', $val[0],$val[1]];
			}elseif($k=='arr_date'){
				if(!$val['0'] || !$val['1']) continue;
				$whereAnd[] = ['between', 'arr', $val[0],$val[1]];
			}elseif($k=='order_state'){
				if($val !== ""){
					$where[$k] = $val;
				}
			}else
			{
				$where[$k] = $val;
			}
		}
		return array('where'=>$where,'whereAnd'=>$whereAnd);
	}

	//订单详情
	public function actionOrderDetail()
	{
		if(!$orderId = $this->request('order_id'))
			return Tool::showRes([400, '参数有误']);
		$where = ['order_id'=>$orderId];
		$field = ['*'];
		$extend= array('order_goods','order_video_goods','order_common','order_jiagou_goods');
		$data = OrdersModel::getOrderDetail($where,$field,$extend);
		return Tool::showRes([0, '订单详情'],$data);
	}

	//订单导出
	public function actionExport()
	{

		$where = $this->formartWhere();
		$page  = $this->request('page','1');
		$params = array(
			'field'	=> ['id','order_sn','name','price','member_id','phone','carno','order_state','des','rate','rmnum','arr','dep','create_time','pay_time','pay_price','discount','couponid','couponname'],
			'order' => 'id desc',
			'page'	=> $page,
			'limit' => $this->request('page_size',100000),
		);
		$list = OrdersModel::getOrderList($where,$params);
		$orderState = Yii::$app->params['orderState'];
		//组织导出数据
		$exportData = array();
		foreach($list as $val)
		{
			$temp = [];
			$temp[] = ' '.$val['order_sn'];
			$temp[] = $val['member_id'];
			$temp[] = $val['name'];
			$temp[] = $val['phone'];
			$temp[] = $val['descript'];
			$temp[] = $val['price'];

			$temp[] = $val['pay_price'];
			$temp[] = $val['discount'];
			$temp[] = $val['couponid'];
			$temp[] = $val['couponname'];



			$temp[] = $val['arr'];
			$temp[] = $val['dep'];
			$temp[] = $val['rate'];
			$temp[] = $orderState[$val['order_state']];
			$temp[] = date("Y-m-d H:i:s",$val['create_time']);
			$exportData[] = $temp;
		}
		$headData = array('A1'=>'订单ID','B1'=>'会员ID','C1'=>'会员姓名','D1'=>'会员电话','E1'=>'房型','F1'=>'订单金额','G1'=>'实付金额','H1'=>'优惠金额','I1'=>'优惠券券码','J1'=>'优惠券名称','K1'=>'到达时间','L1'=>'离开时间','M1'=>'房价','N1'=>'订单状态','O1'=>'下单时间');

		$fileName = 'order-'.date('Y-m-d').'.xls';

		$execlObj = new OutputExecl();
		$res = $execlObj->output($headData,$exportData,$fileName);
		if($res)
			$this->out('下载地址',array('url'=>$res));
		else
			$this->error('导出失败');
	}


//修改订单状态
	public function actionEditOrderState(){
		$id = $this->request('id');
		if(empty($id)){
			return Tool::showRes([400, 'id不能为空']);
		}
		$model = new OrdersModel();
		$model = $model::findOne($id);
		$model->order_state = $this->request('order_state');
		if($model->save()) {
			return Tool::showRes([0, '操作成功']);
		}
		return Tool::showRes([400, '操作失败']);
	}
}